//
//  HYToolBar.m
//  HaiYue
//
//  Created by skyline on 2018/1/2.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYToolBar.h"
#import "HYButton.h"

@interface HYToolBar ()
@property (nonatomic, strong) UIView *line;
@property (nonatomic, copy) NSString *middleTitle;
@property (nonatomic, copy) NSString *leftTitle;
@property (nonatomic, copy) NSString *rightTitle;
@property (nonatomic, copy) NSString *leftImage;
@property (nonatomic, copy) NSString *rightImage;
@end
@implementation HYToolBar
- (instancetype)initWithMiddleTitle:(NSString *)middleTitle
                          leftTitle:(NSString *)leftTitle leftImageName:(NSString *)leftImageName
                         rightTitle:(NSString *)rightTitle rightImageName:(NSString *)rightImageName
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HYToolBarHeight)];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.middleTitle = middleTitle;
        self.leftTitle = leftTitle;
        self.rightTitle = rightTitle;
        self.leftImage = leftImageName;
        self.rightImage = rightImageName;
        self.line.hidden = NO;
        self.leftBtn.hidden = NO;
        self.middleBtn.hidden = NO;
        self.rightBtn.hidden = NO;
    }
    return self;
}

- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc]init];
        _line.backgroundColor = CELL_LINE_COLOR;
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_equalTo(CellBottomLineHeight);
        }];
    }
    return _line;
}
- (HYButton *)leftBtn
{
    if (!_leftBtn)
    {
        _leftBtn = [self buttonTitle:self.leftTitle imageName:self.leftImage];
        _leftBtn.tag = 201;
        [self addSubview:_leftBtn];
        [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(5);
            make.size.mas_equalTo(self.leftBtn.bounds.size);
        }];
    }
    return _leftBtn;
}

- (HYButton *)rightBtn
{
    if (!_rightBtn)
    {
        _rightBtn = [self buttonTitle:self.rightTitle imageName:self.rightImage];
        _rightBtn.tag = 203;
        [self addSubview:_rightBtn];
        [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self);
            make.size.mas_equalTo(self.rightBtn.bounds.size);
        }];
    }
    return _rightBtn;
}

- (HYButton *)middleBtn
{
    if (!_middleBtn)
    {
        _middleBtn = [[HYButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - HYToolBarHeight * 2 - GAP * 2, HYToolBarHeight - GAP)];
        [_middleBtn addTarget:self action:@selector(actionForToolBar:) forControlEvents:UIControlEventTouchUpInside];
        [_middleBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_middleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_middleBtn setTitle:self.middleTitle forState:UIControlStateNormal];
        _middleBtn.type = CCShapeLayerTypeEllipse;
        _middleBtn.tag = 202;
        [self addSubview:_middleBtn];
        [_middleBtn  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(self.middleBtn.bounds.size);
        }];
    }
    return _middleBtn;
}

- (HYButton *)buttonTitle:(NSString *)title imageName:(NSString *)imageName
{
    HYButton *button = [[HYButton alloc] initWithFrame:CGRectMake(0, 0, HYToolBarHeight , HYToolBarHeight) title:title titleColor:self.titleColor];
    [button addTarget:self action:@selector(actionForToolBar:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightLight];
    button.position = HYButtonImageUpPosition;
    return button;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    if (_titleColor != titleColor)
    {
        _titleColor = titleColor;
        [self.leftBtn setTitleColor:self.titleColor forState:UIControlStateNormal];
        [self.rightBtn setTitleColor:self.titleColor forState:UIControlStateNormal];
        self.middleBtn.touchColor = self.titleColor;
        self.middleBtn.backgroundColor = self.titleColor;
    }
}

- (void)actionForToolBar:(HYButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(eventWithClicked:)])
    {
        switch (sender.tag)
        {
            case 201:
                [self.delegate eventWithClicked:HYToolBarEventLeftBtnAction];
                break;
            case 202:
                [self.delegate eventWithClicked:HYToolBarEventMiddleBtnAction];
                break;
            case 203:
                [self.delegate eventWithClicked:HYToolBarEventRightBtnAction];
            default:
                break;
        }
    }
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
